#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
# http://docs.python.org/whatsnew/pep-328.html

from __future__ import print_function   # deletes the print statement
from __future__ import unicode_literals # unicode by default

import unittest
from tctable import TCTable

try:
    from unipath import Path
except ImportError:
    print('I recommend "sudo easy_install -UZ Unipath".')
    raise


class TestTCTable(unittest.TestCase):
    def setUp(self):
        path = Path('TEST.tct')
        path.remove() # delete the file
        self.table = table = TCTable(path, 'w')
        table['Ludwig'] = dict(name='Beethoven', age='220', type='person',
                               profession='composer')
        table['Robert'] = dict(name='Schumann', age='180', type='person',
                               profession='composer')
        table['Frederic']=dict(name='Chopin', age='180', type='person',
                               profession='composer')
        table['Claude'] = dict(name='Debussy', age='100', type='person',
                               profession='composer')
        table['Nando'] = {'name':'Florestan', 'age':'33', 'type':'person',
                          'profession':'Python programmer and dilettante'}
    
    def tearDown(self):
        self.table.close()
    
    def test_basic(self):
        t = self.table
        lent = len(t)
        self.assertEquals(lent, 5)
        self.assertEquals(t['Nando'],
                          {'name':'Florestan', 'type':'person', 'age':'33',
                           'profession':'Python programmer and dilettante'})
        assert 'non-existing key' not in t
        assert 'Claude' in t
        # Must be able to put stuff with byte zero
        # ...although there should be a problem querying columns with
        # names that contain \0
        t.keep_or_put('a\0a', {'\0a':'a\0'})
        self.assertEquals(t['a\0a'], {'\0a':'a\0'})
        self.assertEquals('keep', t.keep_or_put('a\0a', {'not':'not'}))
        self.assertEquals(t.get('a\0a'), {'\0a':'a\0'})
        # Test get() with default
        self.assertEquals(t.get('doesnt exist', default=dict()), dict())
        # Test del
        try:
            del t['9']
        except KeyError:
            pass
        else:
            self.fail('del should raise KeyError when key not found.')
        del t['a\0a']
        # Test len()
        self.assertEquals(lent, len(t))
        self.assertEquals(-1, t.size_of('a key that doesnt exist'))
    
    def test_queries(self):
        t = self.table
        query = t.query.col('age').equals('180')
        alist = query.keys()
        #query.close()
        self.assertEquals(alist, ['Robert', 'Frederic'])
        
        query = t.query.contains('an', col='name').order_by('name')
        self.assertEquals(query.keys(), ['Nando', 'Robert'])
        #query.close()
        
        self.assertEquals(t.query.keys(), ['Ludwig', 'Robert', 'Frederic',
                                           'Claude', 'Nando'])
        self.assertEquals(t.query.col('profession') \
                                 .contains_all('Python dilettante').keys(),
                          ['Nando'])
        self.assertEquals(['Nando'],
                          t.query.col('profession').no.endswith('oser').keys())
        t.query.delete_all()
        self.assertEquals(len(t), 0)


if __name__ == '__main__':
    unittest.main()
